package cn.hicard.risk.service.webpay.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.hicard.risk.dao.webpay.TransMonitorDao;
import cn.hicard.risk.service.webpay.TransMonitorService;
import cn.hicard.util.AppConfig;
import cn.hicard.util.WmgwSms;

@Service
public class TransMonitorServiceImpl implements TransMonitorService {
	
	private TransMonitorDao transMonitorDao;

	public void setTransMonitorDao(TransMonitorDao transMonitorDao) {
		this.transMonitorDao = transMonitorDao;
	}

	@Override
	public List<Map<String, Object>> findList(String startTime, String endTime, String maxTransTime) throws Exception {
		return transMonitorDao.findList(startTime, endTime, maxTransTime);
	}

	@Override
	public List<Map<String, Object>> successRateStatistics(String startTime, String endTime) throws Exception {
		Map<String, String> channelMap = new HashMap<String, String>();
		channelMap.put("1001", "广银联");
		channelMap.put("1003", "宁波银联");
		channelMap.put("1007", "杭州银联");
		channelMap.put("1009", "福建银联");
		channelMap.put("2001", "快钱");
		
		double GYLSuccessRate = Double.parseDouble(AppConfig.get("GYLSuccessRate"));
		double NBYLSuccessRate = Double.parseDouble(AppConfig.get("NBYLSuccessRate"));
		double HZYLSuccessRate = Double.parseDouble(AppConfig.get("HZYLSuccessRate"));
		double FJYLSuccessRate = Double.parseDouble(AppConfig.get("FJYLSuccessRate"));
		double KQSuccessRate = Double.parseDouble(AppConfig.get("KQSuccessRate"));
		double nullChannelSuccessRate = Double.parseDouble(AppConfig.get("nullChannelSuccessRate"));
		
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		resultList = transMonitorDao.successRateStatistics(startTime, endTime);
		
		if(resultList != null && resultList.size()> 0){
			for(Map resultMap : resultList){
				double successRate = Double.parseDouble(resultMap.get("successRate").toString());
				String channel = resultMap.get("channel").toString();
				String sumCount = resultMap.get("sumCount").toString();
				String failCount = resultMap.get("failCount").toString();
				//当返回的成功率低于给定的值时，会发送短信到指定的手机号码
				if("1001".equals(channel) && successRate < GYLSuccessRate){
					sendMsg(channelMap.get(channel), sumCount, failCount, successRate, GYLSuccessRate);
				} else if("1003".equals(channel) && successRate < NBYLSuccessRate){
					sendMsg(channelMap.get(channel), sumCount, failCount, successRate, NBYLSuccessRate);
				} else if("1007".equals(channel) && successRate < HZYLSuccessRate){
					sendMsg(channelMap.get(channel), sumCount, failCount, successRate, HZYLSuccessRate);
				} else if("1009".equals(channel) && successRate < FJYLSuccessRate){
					sendMsg(channelMap.get(channel), sumCount, failCount, successRate, FJYLSuccessRate);
				} else if("2001".equals(channel) && successRate < KQSuccessRate){
					sendMsg(channelMap.get(channel), sumCount, failCount, successRate, KQSuccessRate);
				} else if(( null == channel || "".equals(channel)) && successRate < nullChannelSuccessRate){
					sendMsg("为空", sumCount, failCount, successRate, nullChannelSuccessRate);
				} else{
					System.out.println("成功率高于期望值！");
				}
			}
		}
		return resultList;
	}
	
	private void sendMsg(String channel,String sumCount, String failCount, double successRate, double rate){
		 String mobilePhoneNo = AppConfig.get("mobilePhoneNo");
		 String[] mobilePhoneNoArray = mobilePhoneNo.split(",");
		 String dateTime = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date(System.currentTimeMillis() - 30000));
		 
		 StringBuffer message = new StringBuffer(dateTime + "，过去10分钟内，交易渠道：");
		 message.append(channel);
		 message.append("，总交易笔数：").append(sumCount);
		 message.append("笔，失败笔数：").append(failCount);
		 message.append("笔，交易成功率:").append(successRate);
		 message.append("，低于期望值:").append(rate).append("。");
		 
		//调用发送短信的方法，确定好短信的内容
		if(mobilePhoneNoArray != null && mobilePhoneNoArray.length > 0){
			for(int i = 0; i < mobilePhoneNoArray.length; i++){
				WmgwSms.sendMsg(message.toString(), mobilePhoneNoArray[i]);
			}
		}
	}

	@Override
	public List<Map<String, Object>> exceptionStatistics(String transDate, String startTime, String endTime,String msgcode) throws Exception {
		// TODO Auto-generated method stub
		if(transDate == null || "".equals(transDate)){
			transDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		}
		if(startTime == null || "".equals(startTime)) {
			startTime = "00:00:00";
		}
		if(endTime == null || "".equals(endTime)) {
			endTime = "23:59:59";
		}
		if(msgcode == null || "".equals(msgcode)){
			msgcode = "0200";
		}
		return transMonitorDao.exceptionStatistics(transDate, startTime, endTime, msgcode);
	}

}
